//
//  TripDetailController.m
//  XhlTrip
//
//  Created by dllo on 15/8/28.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "TripDetailController.h"
#import "LeaonWorking.h"
#import "TripDetialCell.h"
#import "PlanDetailHeader.h"
#import "UIImageView+WebCache.h"
#import "UMSocial.h"
#import "PhotoViewController.h"
#import "MBProgressHUD.h"


@interface TripDetailController ()<UITableViewDataSource, UITableViewDelegate, UMSocialUIDelegate>
@property (nonatomic, retain) NSMutableDictionary *resultDic;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) PlanDetailHeader *tripHeader;
@property (nonatomic, retain) NSString *deltext;
@property (nonatomic, retain) NSMutableArray *photoArr;


@end

@implementation TripDetailController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.resultDic = [NSMutableDictionary dictionary];
    }
    return self;
}


- (void)passNetId:(NSString *)netId nsstring:(NSString *)deslabel
{
    
    self.deltext = deslabel;
    
    /* 执行它的时候别的都不执行 所以要把传过来的参数拿到他的上边 */
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
    hud.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:hud];
    hud.labelText = @"正在加载";
    [hud show:YES];
    NSString *path = [NSString stringWithFormat:@"%@%@%@", @"https://chanyouji.com/api/attractions/", netId, @".json"];
    NSString *nn = [NSString stringWithFormat:@"%@%@", @"tripDetail", netId];
    [LeaonWorking netWorkingHanderGetGataWithUrl:path pathname:nn resultBlock:^(id result) {
        
        [self.resultDic setDictionary:result];
        
        [self createHeaderView];
        [_tableView reloadData];
        [hud removeFromSuperview];
        [hud release];
      
    }];
}

- (void)createShareButton
{
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 30 * Width, 30 * Height);
    [button addTarget:self action:@selector(shareButton:) forControlEvents:UIControlEventTouchUpInside];
    [button setBackgroundImage:[self nameWithPath:@"share.png"] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    
}

/* 收藏点击事件 */
- (void)shareButton:(UIButton *)button
{
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"55e3f7f967e58ee85b00110d"
                                      shareText:[self.resultDic objectForKey:@"description"]
                                     shareImage:self.tripHeader.mage.image
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent, UMShareToRenren, UMShareToDouban, UMShareToWechatSession,nil]
                                       delegate:self];
}

- (UIImage *)nameWithPath:(NSString *)paths
{
    NSString *path = [[NSBundle mainBundle] pathForResource:paths ofType:nil];
    UIImage *image = [UIImage imageWithContentsOfFile:path];
    return image;
}

- (void)createTableView
{
   
    CGFloat height = [PlanDetailHeader heightWithText:self.deltext width:self.view.frame.size.width - 20 * Height];
    self.tripHeader = [[[PlanDetailHeader alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, height + 195 * Height)] autorelease];
    
     self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.tableView.rowHeight = 220 * Height;
    self.tableView.tableHeaderView = self.tripHeader;
    [self.view addSubview:self.tableView];
    [_tableView release];
    

}

- (void)createHeaderView
{
    self.tripHeader.desLabel.text = [self.resultDic objectForKey:@"description"];
    [self.tripHeader.mage sd_setImageWithURL:[self.resultDic objectForKey:@"image_url"]];
    self.tripHeader.nameLabel.text = [self.resultDic objectForKey:@"name_zh_cn"];
    
    if (![[self.resultDic objectForKey:@"name_en"] isKindOfClass:[NSNull class]]) {
        self.tripHeader.countLabel.text = [self.resultDic objectForKey:@"name_en"];
    }
    
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    /* 给cell传过来一个字典 */
    NSDictionary *dic = [[self.resultDic objectForKey:@"attraction_trip_tags"] objectAtIndex:indexPath.section];
    NSMutableDictionary *rowDic = [[dic objectForKey:@"attraction_contents"] objectAtIndex:indexPath.row];
    static NSString *resue = @"reu";
    TripDetialCell *cell = [tableView dequeueReusableCellWithIdentifier:resue];
    if (cell == nil) {
        cell = [[[TripDetialCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:resue] autorelease];
           }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    cell.tipDic = rowDic;
  
    /* 推出下一界面 */
    
    [cell createbutton:rowDic];
    cell.block = ^(NSArray *array, NSInteger y){
        
        PhotoViewController *photo = [[PhotoViewController alloc] init];
        photo.h = y - 1;
        [photo.phototArr addObjectsFromArray:array];
        [self presentViewController:photo animated:YES completion:^{
        }];
        [photo release];

        
    };

    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [[self.resultDic objectForKey:@"attraction_trip_tags"] count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSMutableArray *sectionArr = [self.resultDic objectForKey:@"attraction_trip_tags"];
    NSMutableArray *rowArr = [sectionArr[section] objectForKey:@"attraction_contents"];
    return rowArr.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSMutableDictionary *section1 = [[self.resultDic objectForKey:@"attraction_trip_tags"] objectAtIndex:section];
    NSString *titlename = [section1 objectForKey:@"name"];
    return titlename;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    /* 取出文字 */
    NSDictionary *dic = [[self.resultDic objectForKey:@"attraction_trip_tags"] objectAtIndex:indexPath.section];
    NSDictionary *rowDic = [[dic objectForKey:@"attraction_contents"] objectAtIndex:indexPath.row];

    CGFloat textHeight = [TripDetialCell heightWithText:[rowDic objectForKey:@"description"] width:self.view.frame.size.width - 20 * Width];
    
    return textHeight + 140 * Height;
}


- (void)dealloc
{
    [_deltext release];
    [_photoArr release];
    [_tableView release];
    [_tripHeader release];
    [_resultDic release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTableView];
    [self createShareButton];
    
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
